<?php
class IndexController extends Zend_Controller_Action {
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;
	protected $_sections;
	protected $_baseLanguage;

	public function init() {
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "index";

		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );

		$this->_utilities = new Utilities ( );
		$this->view->assign ( "utilities", $this->_utilities );

		$section = new SectionModel ( );
		$sections = $section->getAll();
		$this->view->assign ( "sections", $sections );
		
		$commodity = new CommodityModel();
		$sec_items = array();		
		foreach ($sections as $sec){
			$items = $commodity->getCommodityBySectionId($sec->sec_id, 5);
			$sec_items[$sec->sec_id] = $items;
		}
		$this->view->assign("sec_items", $sec_items);
		
		$category = new CategoryModel();
		$this->view->assign("categories", $category->getAll());
		
		//Newly param for tab controller
		$this->view->assign("isHomepage", true);
		
		// New commods
		$commodity = new CommodityModel();
		$listOfCommods = $commodity->getAllCommodity(1, 10);
		$this->view->assign("new_commods", $listOfCommods);
	}

	public function indexAction() {
	}

	public function vipAction() {
		// Disable the renderer
		$this->_helper->viewRenderer->setNoRender();

		// Retrieve content and create an XML
		$this->_response->setHeader('Content-Type', 'text/xml');
		$this->_response->setHeader('Cache-Control', 'no-cache');
		$this->_response->setHeader('Pragma', 'no-cache');

		$type = $this->_request->getParam('type');

		$xml = '<vip>';
		
		$commodity = new CommodityModel();
		if ($type == 'need') {
			// Get VIP commodities with type of need
			$commodities = 	$commodity->getVipBuyCommodity(5);
		} else if ($type == 'have') {
			// Get VIP commodities with type of have
			$commodities = $commodity->getVipCommodity(5);
		}

		// Then build the xml
		foreach ($commodities as $vip){
			$image = $vip->img_path.$vip->img_title;
			if (!$image){
				$image = $this->_baseUrl . "/public/images/frontend/no_photo.jpg";
			}
			$date = $this->_utilities->formatdate($vip->com_posted_date);
			$text = $this->_utilities->htmlFilter($vip->com_name);
			$xml .= '<commod image="'.$image.'" link="#commod" date="'.$date.'" text="'.$text.'" />';
		}

		$xml .= '</vip>';

		echo $xml;
	}

	public function newsAction() {
		// Disable the renderer
		$this->_helper->viewRenderer->setNoRender();

		// Start the function
		$this->_response->setHeader('Content-Type', 'text/xml');
		$this->_response->setHeader('Cache-Control', 'no-cache');
		$this->_response->setHeader('Pragma', 'no-cache');
		
		$article = new ArticleModel();
		$newsList = $article->getAllArticleForView(10);
		
		$html = '<items>';
		foreach ($newsList as $news) {
			$text = $this->_utilities->htmlFilter($news->art_title);
			
			$article_title = $this->_utilities->cv2urltitle(htmlspecialchars (trim($news->art_title)));
			$param = str_ireplace(" ", "-", trim($article_title))."/".date('Y')."/".date('m')."/".$news->art_id.""."-1.top";
			$link = $this->_baseUrl . '/news/view/' . $param;
			
			$html .= '<item link="'.$link.'" text="'.$text.'" />';
		}
		$html .= '</items>';

		echo $html;
	}

	// Build the search panel
	public function ajaxsearchAction() {
		// Disable the renderer
		$this->_helper->viewRenderer->setNoRender();

		$pane = $this->_request->getParam('pane');
		$this->_response->setHeader('Content-Type', 'text/html');

		if ($pane == 'advanced') {
			Zend_Loader::loadFile('./app/modules/guest/views/scripts/box_search_advanced_pane.phtml');
		} else if ($pane == 'help') {
			Zend_Loader::loadFile('./app/modules/guest/views/scripts/box_search_help_pane.phtml');
		}
	}

}//End class.